package cn.tedu.charging.order.config;

import cn.tedu.charging.common.constant.AmqpComponentConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class AmqpDeclareConfig {
    // 声明业务交换机
    @Bean
    public Exchange delayBillEx() {
        return ExchangeBuilder.fanoutExchange(AmqpComponentConst.DELAY_BILL_EX).build();
    }

    // 声明业务队列
    @Bean
    public Queue delayBillQueue() {
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_QUEUE)
                // 延迟消息的过期时间，单位毫秒
                .withArgument("x-message-ttl", 1000 * 60)
                // 绑定死信交换机
                .withArgument("x-dead-letter-exchange", AmqpComponentConst.DELAY_BILL_DL_EX)
                // 绑定死信路由键
                .withArgument("x-dead-letter-routing-key", AmqpComponentConst.DELAY_BILL_DL_RK)
                .build();
    }

    // 声明业务队列和业务交换机的绑定关系
    @Bean
    public Binding delayBillQueueBinding() {
        return BindingBuilder.bind(delayBillQueue()).to(delayBillEx()).with("").noargs();
    }

    // 声明死信交换机
    @Bean
    public Exchange delayBillDlEx() {
        return ExchangeBuilder.directExchange(AmqpComponentConst.DELAY_BILL_DL_EX).build();
    }

    // 声明死信队列
    @Bean
    public Queue delayBillDlQueue() {
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_DL_QUEUE).build();
    }

    // 声明死信队列和业务交换机的绑定关系
    @Bean
    public Binding delayBillDlQueueBinding() {
        return BindingBuilder.bind(delayBillDlQueue()).to(delayBillDlEx()).with(AmqpComponentConst.DELAY_BILL_DL_RK).noargs();
    }
}
